﻿using HaiFengShop.Entity;
using HaiFengShop.ORM;
using HaiFengShop.ORM.Models;
using HaiFengShop.Tools;
using HaiFengShop.Tools.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace HaiFengShop.Service
{
    public class IndexService
    {
        private readonly SqlSugarClient Context;
        private readonly ResponseMessage res;
        public IndexService(ResponseMessage responseMessage,
             SugarDbContext sugarDbContext)
        {
            this.Context = sugarDbContext.Db;
            this.res = responseMessage;
        }

        public ResponseMessage AppInfo()
        {

            var currentTime = DateTimeExtensions.ConvertToUnixTimestamp(DateTime.Now);
            var banner = Context.Queryable<tb_ad>()
                .Where(s => s.enabled == 1 && s.is_delete == 0)
                .OrderBy(s => s.sort_order, OrderByType.Asc)
                .ToList();
            var notice = Context.Queryable<tb_notice>()
                .Where(s => s.is_delete == 0)
                .ToList();



            var channel =
                Context.Queryable<tb_category>()
            .Where(s => s.is_channel == 1 && s.parent_id == 0)
                .OrderBy(s => s.sort_order).ToList();

            var categoryList = Context.Queryable<tb_category>()
            .Where(s => s.is_show == 1 && s.parent_id == 0)
                .OrderBy(s => s.sort_order).ToList();
            List<DTONewCategoryList> newCategoryList = new List<DTONewCategoryList>();

            foreach (var categoryItem in categoryList)
            {
                var categoryGoods = Context.Queryable<tb_goods>().Where(s =>
                s.category_id == categoryItem.id
                && s.is_on_sale == 1
                && s.goods_number >= 0
                && s.is_index == 1
                && s.is_delete == 0)
                    .OrderBy(s => s.sort_order).ToList();


                newCategoryList.Add(new DTONewCategoryList()
                {
                    id = categoryItem.id,
                    name = categoryItem.name,
                    goodsList = categoryGoods,
                    banner = categoryItem.img_url,
                    height = categoryItem.p_height
                });
            }
            res.data = new
            {
                channel,
                banner,
                notice,
                categoryList = newCategoryList,
            };
            return res;

        }
    }
}
